package com.hilinos.ftp.core;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;

import com.hilinos.common.util.EntityUtils;
import com.hilinos.ftp.config.ServerConfig;
import com.hilinos.ftp.mapper.UserMapper;
import com.hilinos.ftp.pojo.UserPojo;

import org.apache.ftpserver.ftplet.DefaultFtplet;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.FtpReply;
import org.apache.ftpserver.ftplet.FtpRequest;
import org.apache.ftpserver.ftplet.FtpSession;
import org.apache.ftpserver.ftplet.FtpletResult;
import org.apache.ftpserver.impl.DefaultFtpSession;
import org.apache.ftpserver.impl.LocalizedDataTransferFtpReply;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class MyFtplet extends DefaultFtplet {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ServerConfig serverConfig;

    @Override
    public FtpletResult afterCommand(FtpSession session, FtpRequest request, FtpReply reply) throws FtpException, IOException {
        log.info(EntityUtils.toJsonString(request));
        if ("PASS".equals(request.getCommand())) {
            UserPojo user = userMapper.selectById(session.getUser().getName());
            if (user != null) {
                if (request.getArgument().equals(user.getUserpassword())) {
                    try {
                        String userName = session.getUser().getName();
                        String userDir = null;
                        if ("admin".equals(user.getUsertype())) {
                            userDir = serverConfig.getRootDir() + File.separator;
                        } else {
                            userDir = serverConfig.getRootDir() + File.separator + userName + File.separator;
                        }
                        new File(userDir).mkdirs();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return super.afterCommand(session, request, reply);
    }

    @Override
    public FtpletResult beforeCommand(FtpSession session, FtpRequest request) throws FtpException, IOException {
        return super.beforeCommand(session, request);
    }

    @Override
    public FtpletResult onConnect(FtpSession session) throws FtpException, IOException {
        return super.onConnect(session);
    }

    @Override
    public FtpletResult onLogin(FtpSession session, FtpRequest request) throws FtpException, IOException {
        return super.onLogin(session, request);
    }

    @Override
    public FtpletResult onUploadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
        String fileAbsolutePath = null;
        try {
            DefaultFtpSession defaultFtpSession = (DefaultFtpSession) session;
            Field field = defaultFtpSession.getClass().getDeclaredField("ioSession");
            field.setAccessible(true);
            IoSession ioSession = (IoSession) field.get(defaultFtpSession);
            Field replyField = ioSession.getClass().getDeclaredField("lastReply");
            replyField.setAccessible(true);
            LocalizedDataTransferFtpReply reply = (LocalizedDataTransferFtpReply) replyField.get(ioSession);
            File ftpFile = (File) reply.getFile().getPhysicalFile();
            fileAbsolutePath = ftpFile.getAbsolutePath();
        } catch (Exception e) {
            log.error("文件上传成功但是入库失败", e);
        }
        return super.onUploadEnd(session, request);
    }

}
